home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / random.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  3.7 KB  |  106 lines

  1. #ifndef __RANDOM_H
  2. #define __RANDOM_H
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * random.h - Header for the Standard Library random generator
  7.  *
  8.  * $Id: random.h,v 1.6 1996/08/28 01:30:28 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * Copyright (c) 1994
  13.  * Hewlett-Packard Company
  14.  *
  15.  * Permission to use, copy, modify, distribute and sell this software
  16.  * and its documentation for any purpose is hereby granted without fee,
  17.  * provided that the above copyright notice appear in all copies and
  18.  * that both that copyright notice and this permission notice appear
  19.  * in supporting documentation.  Hewlett-Packard Company makes no
  20.  * representations about the suitability of this software for any
  21.  * purpose.  It is provided "as is" without express or implied warranty.
  22.  *
  23.  *
  24.  ***************************************************************************
  25.  *
  26.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  27.  * ALL RIGHTS RESERVED *
  28.  * The software and information contained herein are proprietary to, and
  29.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  30.  * intends to preserve as trade secrets such software and information.
  31.  * This software is furnished pursuant to a written license agreement and
  32.  * may be used, copied, transmitted, and stored only in accordance with
  33.  * the terms of such license and with the inclusion of the above copyright
  34.  * notice.  This software and information or any other copies thereof may
  35.  * not be provided or otherwise made available to any other person.
  36.  *
  37.  * Notwithstanding any other lease or license that may pertain to, or
  38.  * accompany the delivery of, this computer software and information, the
  39.  * rights of the Government regarding its use, reproduction and disclosure
  40.  * are as set forth in Section 52.227-19 of the FARS Computer
  41.  * Software-Restricted Rights clause.
  42.  * 
  43.  * Use, duplication, or disclosure by the Government is subject to
  44.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  45.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  46.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  47.  * P.O. Box 2328, Corvallis, Oregon 97339.
  48.  *
  49.  * This computer software and information is distributed with "restricted
  50.  * rights."  Use, duplication or disclosure is subject to restrictions as
  51.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  52.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  53.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  54.  * then the "Alternate III" clause applies.
  55.  *
  56.  **************************************************************************/
  57.  
  58. #include <stdcomp.h>
  59. #include "rw/stddefs.h"   
  60. #ifdef _RWSTD_MULTI_THREAD
  61. #include "rw/stdmutex.h"
  62. #endif
  63.  
  64. #ifndef _RWSTD_NO_NAMESPACE
  65. namespace __rwstd {
  66. #endif
  67.  
  68. class _RWSTDExport  __random_generator
  69. {
  70.   protected:
  71.  
  72.     enum { LENGTH = 55 };
  73.  
  74.     unsigned long table[LENGTH];
  75.     size_t        index1;
  76.     size_t        index2;
  77. #ifdef _RWSTD_MULTI_THREAD
  78.     _RWSTDMutex    mutex;
  79. #endif
  80.  
  81.     void seed (unsigned long j);
  82.  
  83.   public:
  84.  
  85.     unsigned long operator() (unsigned long limit)
  86.     {
  87. #ifdef _RWSTD_MULTI_THREAD
  88.         _RWSTDGuard guard(mutex);
  89. #endif
  90.         index1 = (index1 + 1) % LENGTH;
  91.         index2 = (index2 + 1) % LENGTH;
  92.         table[index1] = table[index1] - table[index2];
  93.         return table[index1] % limit;
  94.     }
  95.  
  96.     __random_generator (unsigned long j) { this->seed(j); }
  97.    
  98. };
  99.  
  100. #ifndef _RWSTD_NO_NAMESPACE
  101. }
  102. #endif
  103.  
  104. #pragma option pop
  105. #endif /* __RANDOM_H */
  106.